package com.learn.spring5_demo6.dao;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.learn.spring5_demo6.entity.Book;

@Repository
public class BookDaoImpl implements BookDao
{

	// 注入JdbcTemplate
	@Autowired
	private JdbcTemplate jdbcTemplate;

	// 添加的方法
	public void add(Book book)
	{
		// 1. 创建sql语句
		String sql = "insert into t_book values(?,?,?)";

		// 2. 调用方法实现
		Object[] args =
		{ book.getUserId(), book.getUsername(), book.getUstatus() };
		int update = jdbcTemplate.update(sql, args);
		System.out.println(update);

	}

	// 修改
	public void updateBook(Book book)
	{
		String sql = "update t_book set username=?,ustatus=? where user_id=?";

		Object[] args =
		{ book.getUsername(), book.getUstatus(), book.getUserId() };

		int update = jdbcTemplate.update(sql, args);
		System.out.println(update);

	}

	// 删除
	public void delete(String id)
	{
		String sql = "delete from t_book where user_id=?";

		int update = jdbcTemplate.update(sql, id);
		System.out.println(update);
	}

	// 查询表记录条数
	public int selectCount()
	{
		String sql = "select count(*) from t_book";

		Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
		return count;
	}

	// 查询返回对象
	public Book findBookInfo(String id)
	{
		String sql = "select * from t_book where user_id=?";
		Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
		return book;
	}

	// 查询返回集合
	public List<Book> findAllBook()
	{
		String sql = "select * from t_book";
		List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
		return bookList;
	}

	// 批量添加
	public void batchAddBook(List<Object[]> batchArgs)
	{
		String sql = "insert into t_book values(?,?,?)";
		int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
		// 打印影响行数
		System.out.println(Arrays.toString(ints));
	}

	// 批量修改
	public void batchUpdateBook(List<Object[]> batchArgs)
	{
		String sql = "update t_book set username=?,ustatus=? where user_id=?";
		int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
		// 打印影响行数
		System.out.println(Arrays.toString(ints));
	}

	// 批量删除
	public void batchDeleteBook(List<Object[]> batchArgs)
	{
		String sql = "delete from t_book where user_id=?";
		int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
		System.out.println(ints);
	}

}
